//package com.zy.controller;
//
//import cn.hutool.captcha.CaptchaUtil;
//import cn.hutool.captcha.ShearCaptcha;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.zy.common.LoginToken;
//import com.zy.domain.User;
//import com.zy.param.LoginParam;
//import com.zy.param.RegisterParam;
//import com.zy.service.UserService;
//import com.zy.utils.HashMD5Util;
//import com.zy.vo.UserVo;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.authc.pam.UnsupportedTokenException;
//import org.apache.shiro.subject.Subject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//
//import org.apache.shiro.authc.AccountException;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.CredentialsException;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Date;
//
//
//@RestController
//public class LoginController {
//
//  @Autowired
//  private UserService userService;
//
//  @GetMapping("/demo")
//  public String Test() {
//    return "ok";
//  }
//
////  @GetMapping("/login")
////  public ResponseEntity<String> toLogin() {
////    return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户还没有登录，请先登录!");
////  }
//
//  /**
//   * 验证码的处理
//   */
////  @RequestMapping("captcha")
//  @GetMapping("captcha")
//  public void captcha(String key, HttpServletResponse response) throws IOException {
//    //使用hutool生成一个验证码
//    //定义图形验证码的长、宽、验证码字符数、干扰线宽度
//    ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(100, 38, 4, 4);
////ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
////图形验证码写出，可以写出到文件，也可以写出到流
//    //保存到Redis
//    if(StringUtils.isNotBlank(key)){
////      ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
////      opsForValue.set(key,captcha.getCode());
////      RedisOperations<String, String> operations = opsForValue.getOperations();
////      //设置60秒过期
////      operations.expire(key,60, TimeUnit.SECONDS);
//      captcha.write(response.getOutputStream());
//    }else{
////      log.error("验证码的key不能为空login/captcha");
//      System.out.println("验证码的key不能为空login/captcha");
//    }
//  }
//  @RequestMapping("/login")
//  @ResponseBody
//  public LoginToken doLogin(@RequestBody  LoginParam loginParam) {
////        checkLoginParam(loginParam);
//      String username = loginParam.getUsername();
//      String pwd = HashMD5Util.getMd5(loginParam.getPassword(), 2);
//      User user = userService.selectUser(username, pwd);
//      System.out.println(user);
//      if (loginParam.getUsername() == null || loginParam.getPassword() == null) {
////        throw new RuntimeException("请输入用户名和密码！");
//        return new LoginToken(-1, "用户名或密码为空", null);
//      }
//      if (null == user) {
////        throw new RuntimeException("用户不存在，请重新登录！");
//        return new LoginToken(-1, "用户名或密码不正确", null);
//      }
//      return new LoginToken(0, "登陆成功", user.getId());
////      UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginParam.getUsername(), loginParam.getPassword());
////      Subject subject = SecurityUtils.getSubject();
////
////      subject.login(usernamePasswordToken); // 做登录
////      // 登录成了，它把用户的登录信息写在sessions 里面，而sessions 里面key时SessionId
////      String token = subject.getSession().getId().toString();
////      // 只要有上面的key，我就是登录过来
////      return ResponseEntity.ok(token);
////      return ResponseEntity.ok("登录成功！");
//
//
//  }
//
////  private void checkLoginParam(LoginParam loginParam) {
////    String username = loginParam.getUsername();
////    String pwd = HashMD5Util.getMd5(loginParam.getPassword(),2);
////    User user = userService.selectUser(username,pwd);
////    System.out.println(user);
////    if (loginParam.getUsername() == null || loginParam.getPassword() == null) {
////      throw new RuntimeException("请输入用户名和密码！");
////    }
////    if (null == user) {
////      throw new RuntimeException("用户不存在，请重新登录！");
////    }
////  }
//
//  @PostMapping("/getPwd")
//  public ResponseEntity<String> getBackPassword(@RequestBody UserVo userVo) {
//
//    checkUserVo(userVo);
//    User userByPhone = userService.getUserByPhone(userVo.getPhoneNum());
//    userByPhone.setPassword(userVo.getPassword());
//    userByPhone.setUpdateDate(new Date());
//    userService.updateById(userByPhone);
//    return ResponseEntity.ok("密码已找回，请重新登录！");
//  }
//
//  private void checkUserVo(UserVo userVo) {
//    String phoneNum = userVo.getPhoneNum();
//    String firstInputPwd = userVo.getFirstInputPwd();
//    String secondInputPwd = userVo.getSecondInputPwd();
//    String verificationCode = userVo.getVerificationCode();
//    if (phoneNum == null) {
//      throw new RuntimeException("请先输入电话号码！");
//    }
//    if (firstInputPwd == null) {
//      throw new RuntimeException("请输入新密码！");
//    }
//    if (secondInputPwd == null) {
//      throw new RuntimeException("请输入新密码！");
//    }
//    if (verificationCode == null) {
//      throw new RuntimeException("请输入发送您手机的验证码！");
//    }
//    if (!firstInputPwd.equals(secondInputPwd)) {
//      throw new RuntimeException("您输入的密码不一致，请重新输入！");
//    }
//  }
//
//  @PostMapping("/register")
//  public User register(
//    @RequestBody @Validated RegisterParam registerParam
//  ) {
//    checkRegisterParam(registerParam);
//    User user = userService.selectUserByPhone(registerParam.getPhoneNum());
//    if (user!=null){
//      throw  new RuntimeException("用户已注册！");
//    }
//    User newUser = userService.registerUser(registerParam);
//    if (newUser==null){
//      return null;
//    }
//    else {
//      System.out.println("注册用户为："+newUser);
//      return newUser;
//    }
//
//  }
//
//  @PostMapping("/reset")
//  public User reset(
//    @RequestParam String phoneNum
//  ) {
//    checkPhoneNum(phoneNum);
//    User user1 = userService.selectUserByPhone(phoneNum);
//    if (user1==null){
//      throw  new RuntimeException("您手机绑定的用户不存在！");
//    }
//    User user2 = userService.resetUserByPhone(phoneNum);
//    System.out.println("手机号为："+phoneNum+"的用户"+user1+"已重置，现在用户为"+user2);
//    return user2;
//  }
//
//  private void checkPhoneNum(String phoneNum) {
//    if (phoneNum==null){
//      throw  new RuntimeException("您输入的手机号不能为空！");
//    }
//    if (phoneNum.length()!=11){
//      throw  new RuntimeException("请输入11位数的手机号！");
//    }
//
//
//  }
//
//  private void checkRegisterParam(RegisterParam registerParam) {
//    //注册表单的昵称
//    String nickname = registerParam.getNickName();
//    //注册表单的手机号
//    String phoneNum = registerParam.getPhoneNum();
//    //注册表单的短信验证码
//    String verificationCode = registerParam.getVerificationCode();
//    //注册表单的密码
//    String password = registerParam.getPassword();
//    //注册表单的推荐人手机号
//    String parentPhone = registerParam.getParentPhone();
//
////    if (nickname==null||phoneNum==null||verificationCode==null||password==null||parentPhone==null){
////      throw new RuntimeException("");
////    }
//    if (nickname == null) {
//      throw new RuntimeException("请输入昵称！");
//    }
//    if (phoneNum == null) {
//      throw new RuntimeException("请输入手机号！");
//    }
//    if (verificationCode == null) {
//      throw new RuntimeException("请输入发送您手机上的短信验证码！");
//    }
//    if (password == null) {
//      throw new RuntimeException("请输入要注册的密码！");
//    }
//    if (parentPhone == null) {
//      throw new RuntimeException("请输入推荐人手机号！");
//    }
//  }
//}